/**
 * Created by sa on 2016/9/22.
 */
var links = [];
var casper = require('casper').create();

function getLinks() {
  var links = document.querySelectorAll('h3.r a');
  return Array.prototype.map.call(links, function(e) {
    return e.getAttribute('href');
  });
}

casper.start('https://www.google.com/ncr', function() {
  // Wait for the page to be loaded
  this.waitForSelector('form[action="/search"]');
});

casper.then(function() {
  // search for 'casperjs' from google form
  this.fill('form[action="/search"]', { q: 'casperjs' }, true);
});

casper.then(function() {
  // aggregate results for the 'casperjs' search
  links = this.evaluate(getLinks);
  // now search for 'phantomjs' by filling the form again
  this.fill('form[action="/search"]', { q: 'phantomjs' }, true);
});

casper.then(function() {
  // aggregate results for the 'phantomjs' search
  links = links.concat(this.evaluate(getLinks));
});

casper.run(function() {
  // echo results in some pretty fashion
  this.echo(links.length + ' links found:');
  this.echo(' - ' + links.join('\n - ')).exit();
});

/**
- /url?q=http://casperjs.org/&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQFggUMAA&usg=AFQjCNHq6IAs8Qmi3AHLHdNjktEWzeq4nQ

 - /url?q=http://docs.casperjs.org/&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQjBAIGzAB&usg=AFQjCNEiNJ-CGdgMW2V4yEdWIoKIjS-KkA
  - /url?q=http://docs.casperjs.org/en/latest/installation.html&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQjBAIHTAE&usg=AFQjCNExYST7gWWscI_7_w66FLfEcQCXYw
  - /url?q=http://docs.casperjs.org/en/latest/modules/casper.html&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQjBAIHzAC&usg=AFQjCNH_5kWJIaW-YCWQdTx6wDShFl2Bzg

  - /url?q=http://docs.casperjs.org/en/latest/modules/&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQjBAIITAF&usg=AFQjCNGh1cr-xF9OCA1vz7t3XNWMFX306A
  - /url?q=http://docs.casperjs.org/en/latest/quickstart.html&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQjBAIIzAD&usg=AFQjCNFVey45dwA-1Y_o28SoIKHogl11JQ
- /url?q=http://docs.casperjs.org/en/latest/selectors.html&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQjBAIJTAG&usg=AFQjCNGIyF5SYvUU_Qfi_oW-uPDuecMwdA
  - /url?q=https://github.com/casperjs/casperjs&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQFggoMAc&usg=AFQjCNExwEPVSMyhiRkRItU9vjx4odt2rw
  - /url?q=http://n1k0.github.io/casperjs/&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQFggtMAg&usg=AFQjCNFuvsFBFq_c1SL7Kowx8EwAraKa_Q
  - /url?q=https://blog.newrelic.com/2013/06/04/simpler-ui-testing-with-casperjs-2/&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQFggyMAk&usg=AFQjCNFrmonUwA0is
L0_Q4rX3A7nDUzptA
- /url?q=https://www.lullabot.com/articles/testing-the-front-end-with-casperjs&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQFgg5MAo&usg=AFQjCNGIhTIF-lofnvKC
6kqiiBoK8lHBHw
- /url?q=https://rubygems.org/gems/casperjs/versions/1.0.0&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQFghAMAs&usg=AFQjCNGrCWSz_IJnyqRnEksmN7fz86Vb2A
  - /url?q=http://stackoverflow.com/questions/tagged/casperjs&sa=U&ved=0ahUKEwjIvrS1g6LPAhXLuo8KHVE3BpEQFghGMAw&usg=AFQjCNHP2d85_rbyZPhnB4F-H3Z6N95eyA
  - /url?q=http://phantomjs.org/&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFggTMAA&usg=AFQjCNHrqSz8rKBQRPcFMnarqQ4pGpe4Uw
  - /url?q=http://phantomjs.org/download.html&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFggeMAE&usg=AFQjCNHEO_lok7UW7Gw75ciLfa2bgR9kaw
  - /url?q=https://github.com/ariya/phantomjs&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFggkMAI&usg=AFQjCNHHnkYK-PncGEFZasObA--xsk2oKA
- /url?q=https://github.com/Medium/phantomjs&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFggqMAM&usg=AFQjCNEEwp3TQGnw6UAShZZaWazvibwsiw
  - /url?q=https://en.wikipedia.org/wiki/PhantomJS&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFggwMAQ&usg=AFQjCNEbOph-PA3HDs-j8tqQK7PbwhP6Dw
  - /url?q=https://scotch.io/tutorials/what-is-phantomjs-and-how-is-it-used&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFgg2MAU&usg=AFQjCNGBrxLvqfNFQHT9-eZQv
M-ZcvwddA
- /url?q=https://www.npmjs.com/package/karma-phantomjs-launcher&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFgg8MAY&usg=AFQjCNEEjsgbgDCOc3KYsivEOkE1v3JTTQ
  - /url?q=http://casperjs.org/&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFghCMAc&usg=AFQjCNHq6IAs8Qmi3AHLHdNjktEWzeq4nQ
  - /url?q=https://phantomjscloud.com/&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFghIMAg&usg=AFQjCNElQPlaSdZXawSJgzLuvR4AVFLwKA
  - /url?q=http://stackoverflow.com/questions/tagged/phantomjs&sa=U&ved=0ahUKEwjksou2g6LPAhUMvI8KHeKAAUQQFghOMAk&usg=AFQjCNG-oHisOK8bf-qXC56N9EGPX8JCCw

**/
